package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysDeviceStateMapper;
import com.ruoyi.system.service.IRelatedTodoeventService;
import com.ruoyi.system.service.ISysDeviceStateService;
import com.ruoyi.system.service.ISysTodoeventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class SysDeviceStateServiceImpl implements ISysDeviceStateService {

    @Autowired
    private SysDeviceStateMapper sysDeviceStateFileMapper;
    @Autowired
    private ISysTodoeventService sysTodoeventService;
    @Autowired
    private IRelatedTodoeventService relatedTodoeventService;


    private String STATEURL;
    @Override
    public List<SysDeviceState> selectSysDeviceStateList(Long deviceId,HttpServletRequest request) {
        StringBuffer requestURL = request.getRequestURL();
        STATEURL = requestURL.toString();
        return sysDeviceStateFileMapper.selectSysDeviceStateList(deviceId);

    }


    @Override
    @Transactional
    public int insertSysDeviceState(HttpServletRequest request,SysDeviceState sysDeviceState) {
        Date date = new Date();
        sysDeviceState.setChangeTime(date);
        sysDeviceState.setCreateTime(date);
//        修改清单中技术规格说明说状态
        LifManageList lifManageList = new LifManageList();
        lifManageList.setTechBook((long) 1);
//        lifManageListService.updateLifManageListByDeviceId();
//        添加代办事项
        SysTodoevent sysTodoevent = new SysTodoevent();
        sysTodoevent.setPublishId(SecurityUtils.getUserId());
        sysTodoevent.setHandleId(sysDeviceState.getApprover());
        sysTodoevent.setRelatedtaskId(sysDeviceState.getLifId());
        sysTodoevent.setLifStatus(0);
        sysTodoevent.setProjectId((Long.valueOf(sysDeviceState.getSysId())));
        sysTodoevent.setStatus(0L);
        sysTodoevent.setReminder("请您审批"+sysDeviceState.getDevName()+"设备技术规格说明书");
        sysTodoevent.setRequestUrl("state");
        sysTodoeventService.insertSysTodoevent(sysTodoevent);
        //向中间表中插入数据
        RelatedTodoevent relatedTodoevent = new RelatedTodoevent();
        relatedTodoevent.setRelatedId(sysDeviceState.getLifId());
        relatedTodoevent.setTodoeventId(sysTodoevent.getId());
        relatedTodoeventService.insertRelatedTodoevent(relatedTodoevent);
//        添加计划书事件
        return sysDeviceStateFileMapper.insertSysDeviceState(sysDeviceState);
    }

    /*
    * 设备状态确定文件上传*/
    @Override
    public Long insertPathName(SysDeviceStateFile stateFile) {
    //添加文件
        sysDeviceStateFileMapper.insertPathName(stateFile);
        return stateFile.getFileId();
    }


    @Override
    public int updateSysDeviceState(SysDeviceState sysDeviceState) {
        Date date = new Date();
        sysDeviceState.setChangeTime(date);
        return sysDeviceStateFileMapper.updateSysDeviceState(sysDeviceState);
    }

    @Override
    public int deleteMarketFileById(Long fileId) {
        return sysDeviceStateFileMapper.deleteMarketFileById(fileId);
    }

    @Override
    public List<SysMarketFile> selectDocFileByFileId(Long id) {
        return sysDeviceStateFileMapper.selectDocFileByFileId(id);
    }

    @Override
    public List<SysDeviceState> selectDeviceStateServiceById(Long devId) {
        return sysDeviceStateFileMapper.selectDeviceStateServiceById(devId);
    }

    @Override
    public SysDeviceState selectDeviceStateServiceByDevId(Long devId) {
        return sysDeviceStateFileMapper.selectDeviceStateServiceByDevId(devId);
    }
}
